package com.lagou.service;

import com.lagou.anno.MyRpcProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@MyRpcProvider
@Service
public class BlogServiceImpl implements IBlogService {

    @Autowired
    private DataSource druidDataSource;

    @Override
    public List<String> findBlog() {
        List<String> result = new ArrayList<>();
        try {
            Connection connection = druidDataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("select * from t_article");
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                String title = resultSet.getString("title");
                System.out.println(title);
                result.add(title);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public void setDruidDataSource(DataSource druidDataSource) {
        this.druidDataSource = druidDataSource;
    }
}
